package com.csi.controller.back;

import cn.hutool.extra.pinyin.PinyinUtil;

import com.csi.domain.Employee;

import com.csi.domain.vo.EmployeeDetail;
import com.csi.service.EmployeeService;
import com.csi.utils.JsonResult;
import com.csi.utils.JwtUtil;
import com.csi.utils.ReturnResults;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@RequestMapping("/employee")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 新增员工
     *
     * @param employee 员工对象
     * @return 请求结果
     */
    @PostMapping(value = "/save", produces = "application/json;charset=utf-8")
    public Object save(@RequestBody Employee employee,HttpServletRequest request) {
        //密码默认身份证号后六位
        String idCard = employee.getIdCard();
        employee.setPassword(idCard.substring(idCard.length() - 6));
        //设置入职时间
        employee.setEntryTime(new Timestamp(System.currentTimeMillis()));
        String token = request.getHeader("token");

       long hotelId = JwtUtil.getTokenBody(token).get("hotelId", Long.class);
        long createId = JwtUtil.getTokenBody(token).get("employeeId", Long.class);
        employee.setHotelId(hotelId);
        employee.setCreateId(createId);
        employee.setUsername(PinyinUtil.getPinyin(employee.getName()).replace(" ","")); //设置姓名拼音为用户名


        int saved = employeeService.save(employee);
        if (saved > 0) {
            return new JsonResult( ReturnResults.ACCESS_SUCCESS,"新增员工成功！");
        }
        return new JsonResult( ReturnResults.ACCESS_FAILED,"新增员工失败！");
    }

    /**
     * 更新员工
     *
     * @param employee 员工对象
     * @return 请求结果
     */
    @PostMapping(value = "/update", produces = "application/json;charset=utf-8")
    public Object update(@RequestBody Employee employee,HttpServletRequest request) {
        String token = request.getHeader("token");
        long handleId = JwtUtil.getTokenBody(token).get("employeeId", Long.class);

        employee.setHandleId(handleId);
        int updated = employeeService.update(employee);
        if (updated > 0) {
            return new JsonResult(ReturnResults.ACCESS_SUCCESS,"更新员工成功！");
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"更新员工失败！");
    }

    /**
     * 删除员工
     *
     * @param employee 员工对象
     * @return 请求结果
     */
    @GetMapping(value = "/delete", produces = "application/json;charset=utf-8")
    public Object delete( Employee employee,HttpServletRequest request) {
        employee.setLeaveTime(new Timestamp(System.currentTimeMillis()));
         long employeeId = JwtUtil.getTokenBody(request.getHeader("token")).get("employeeId", Long.class);

        if (employeeId == employee.getId()){
            return new JsonResult( ReturnResults.ACCESS_FAILED_NO_POWER,"删除员工失败！");
        }
        int deleted = employeeService.delete(employee);
        if (deleted > 0) {
            return new JsonResult( ReturnResults.ACCESS_SUCCESS,"删除员工成功！");
        }
        return new JsonResult( ReturnResults.ACCESS_FAILED,"删除员工失败！");
    }

    /**
     * 修改权限
     * @param employee 员工对象
     * @return
     */
    @PostMapping(value = "/modifyRole", produces = "application/json;charset=utf-8")
    public Object modifyRole(@RequestBody Employee employee,HttpServletRequest request) {
       long employeeId = JwtUtil.getTokenBody(request.getHeader("token")).get("employeeId", Long.class);

        if (employeeId == employee.getId()){
            return new JsonResult(ReturnResults.ACCESS_FAILED_NO_POWER,"删除员工失败！");
        }
        employee.setLeaveTime(new Timestamp(System.currentTimeMillis()));
        employee.setHandleId(employeeId);
        int updated = employeeService.modifyRole(employee);
        if (updated > 0) {
            return new JsonResult(ReturnResults.ACCESS_SUCCESS,"修改权限成功！");
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"修改权限失败！");
    }

    /**
     * 根据员工id查找员工
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/findById", produces = "application/json;charset=utf-8")
    public Object find(@RequestParam("id") long id) {
        EmployeeDetail find = employeeService.findById(id);
        if (find != null) {
            return new JsonResult(ReturnResults.ACCESS_SUCCESS, find,"查找员工成功！");
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"查找员工失败！");
    }

    /**
     * 根据员工名字和酒店id查找员工
     * @param username
     * @param hotelId
     * @return
     */
    @GetMapping(value = "/findByName", produces = "application/json;charset=utf-8")
    public Object findByName(@RequestParam("username") String username,@RequestParam("hotelId") long hotelId){
        EmployeeDetail employee =  employeeService.findByName(username,hotelId);
        if (employee != null) {
            return new JsonResult(ReturnResults.ACCESS_SUCCESS,employee,"查找员工成功！");
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"查找员工失败！");
    }

    /**
     * 查询离职员工
     * @param request
     * @return
     */
    @GetMapping("showLeaveByHotelId")
    public Object findLeaveEmployee(HttpServletRequest request){
       long hotelId = JwtUtil.getTokenBody(request.getHeader("token")).get("hotelId", Long.class);

        List<EmployeeDetail> employees = employeeService.findLeaveEmployee(hotelId);
        if (employees != null && !employees.isEmpty()) {
            return new JsonResult( ReturnResults.ACCESS_SUCCESS, employees,"查找酒店员工成功！");
        }
        return new JsonResult( ReturnResults.ACCESS_FAILED,"查找酒店员工失败");

    }




    /**
     * 模糊查询
     *
     * @param name
     * @param roleId
     * @param sex
     * @return
     */
    @GetMapping(value = "/showByHotelId", produces = "application/json;charset=utf-8")
    public Object like(@RequestParam(value = "name",defaultValue = "null") String name, @RequestParam(name = "roleId",defaultValue = "-1") long roleId, @RequestParam(name = "sex",defaultValue = "-1") int sex,@RequestParam(name = "pageIndex",defaultValue = "1") int pageIndex,@RequestParam(name = "pageSize",defaultValue = "10") int pageSize, HttpServletRequest request) {
        String token = request.getHeader("token");
        long hotelId = JwtUtil.getTokenBody(token).get("hotelId", Long.class);

        PageInfo<EmployeeDetail> employeeList = employeeService.findLike(name, roleId, sex,hotelId,pageIndex,pageSize);
        long count =employeeList.getTotal();
        Map<String, Object> response = new HashMap<>();
        response.put("data", employeeList.getList());
        response.put("count", count);
        if (employeeList.getList() != null ) {
            return new JsonResult( ReturnResults.ACCESS_SUCCESS, response,"查找成功！");
        }
        return new JsonResult( ReturnResults.ACCESS_FAILED,"查找失败！");
    }


    /**
     * 根据角色id查找员工
     * @param roleId
     * @return
     */
    @GetMapping(value = "/findByRole")
    public Object findByRole(@RequestParam("id") long roleId){
        List<Employee> employeeList = employeeService.findByRole(roleId);
        if (employeeList != null && !employeeList.isEmpty()) {
            return new JsonResult( ReturnResults.ACCESS_SUCCESS,employeeList,"查找成功！");
        }
        return new JsonResult( ReturnResults.ACCESS_FAILED,"查找失败！");
    }

    @GetMapping("/userName")
    public Object findUserName(HttpServletRequest request){
        String token = request.getHeader("token");
        long employeeId = JwtUtil.getTokenBody(request.getHeader("token")).get("employeeId", Long.class);
       String userName = employeeService.findUserName(employeeId);
        if (userName != null){
            return new JsonResult(ReturnResults.ACCESS_SUCCESS,userName,"查找成功！");
        }
        return new JsonResult(ReturnResults.ACCESS_FAILED,"查找失败！");

    }

}
